﻿using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Linq;
using System.Runtime.Serialization;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Reflection;

namespace System.ApplicationModel
{
    /// <summary>
    /// Base class for business objects.
    /// </summary>
    [DataContract(IsReference=true)]
    public abstract partial class Entity
    {
        #region Static Methods
        private static object GetDefaultValue(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="Entity"/> class.
        /// </summary>
        protected Entity()
        {

        }
        #endregion

        #region Fields
        private Dictionary<string, object> _backup;
        #endregion

        #region Private Methods
        private void Backup()
        {
            if (!object.ReferenceEquals(this._backup, null))
                return;

            this._backup = new Dictionary<string, dynamic>();

#if SILVERLIGHT
            PropertyInfo[] properties = this.GetType().GetProperties();
            foreach (PropertyInfo property in properties)
            {
                object propValue = property.GetValue(this, null);
                this._backup.Add(property.Name, propValue);
            }
#else
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
            foreach (PropertyDescriptor property in properties)
            {
                object propValue = property.GetValue(this);
                this._backup.Add(property.Name, propValue);
            }
#endif
        }
        #endregion
    }
}
